function install()

%INSTALL 
%
%   INSTALL  crack targetlink calibration tool installation 
%
%   Inputs:
%       -- / --
%
%   Outputs:
%       -- / --
%
%   Syntax:
%       install
%
%   Example:
%       -- / --
%
%   See also:
%       -- / --
% 
%   Author(s): Xiaobiao Huang
%   Copyright 2013-2020 The Xiaobiao Studio.
%   $Revision: 1.0.0.0 $  $Date: 17-Sep-2013 21:50:10 $

fileList = {'tl_open_blockdlg',...
            'tl_manage_doc_ctrls',...
            'tl_task_dlg',...
            'tl_isr_dlg' ,...
            'tl_criticalsection_dlg' ,...
            };
   
%% recreate tl_open_blockdlg.m
filePath = which(fileList{1});
copyfile('xscrack.p',fileparts(filePath));
try
    fid = fopen(filePath,'r+');
    tline = fgets(fid);
    iColumn = 1;
    while ischar(tline)
        farray{iColumn,1} = tline;
        if strfind(tline,'Copyright')
            insertColumnNumber = iColumn + 1;
        end
        iColumn = iColumn + 1;
        tline = fgets(fid);
    end
    insertStr = {['% crack the targetlink calibration' char(10)]; ...
                 ['% add by Xiaobiao Huang' char(10)]; ...
                 ['xscrack(''blklist'',hBlock);' char(10)];
                 ['% end of add crack function' char(10)]; ...
                 char(10);};
    newfile = [farray(1:insertColumnNumber); insertStr; farray(insertColumnNumber+1:end)];
    fclose(fid);
    % create new file
    fid = fopen(filePath,'w+');
    for iline = 1 : length(newfile)
        fwrite(fid,newfile{iline});
    end
catch exception
    fprintf('crack %s failed: %s\n',fileList{1}, exception.message)
    return;
end

fprintf('crack %s successed\n',fileList{1});

%% recreate tl_manage_doc_ctrls.m
for ifile = 2 : length(fileList)
    filePath = which(fileList{ifile});
    farray = '';
    try
        fid = fopen(filePath,'r+');
        tline = fgets(fid);
        iColumn = 1;
        while ischar(tline)
            farray{iColumn,1} = tline;
            if strfind(tline,'dlgdata.pageData{pageNum}.children = Children;')
                insertColumnNumber = iColumn + 1;
            end
            iColumn = iColumn + 1;
            tline = fgets(fid);
        end    
        insertStr = {['        % crack the targetlink calibration' char(10)]; ...
                     ['        % add by Xiaobiao Huang' char(10)]; ...
                     ['        xscrack(''crack'',dlgdata,dlgfig);' char(10)];
                     ['        % end of add crack function' char(10)]; ...
                     char(10);};
    %     newfile = [farray(1:insertColumnNumber-2); insertStr; farray(insertColumnNumber-1:end); insertStr2];
        newfile = [farray(1:insertColumnNumber-2); insertStr; farray(insertColumnNumber-1:end); ];
        fclose(fid);
        % create new file
        fid = fopen(filePath,'w+');
        for iline = 1 : length(newfile)
            fwrite(fid,newfile{iline});
        end
    catch exception
        fclose(fid);
        fprintf('crack %s failed: %s\n',fileList{ifile}, exception.message)
        return;
    end

    fprintf('crack %s successed\n',fileList{ifile});
end
end % end of function  install
